package server.business.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import server.ScriptEnum;
import server.ServerEngin;
import server.business.InformationData;
import server.business.entity.Achievement;
import server.business.entity.Activity;
import server.business.entity.Item;
import server.business.entity.Lottery;
import server.business.entity.Notice;
import server.business.entity.User;
import server.business.login.ILoginScript;
import server.business.service.IHttpService;
import server.commonData.SqlStatementEnum;
import server.commonData.Utils;
import server.jdbc.DAOUtil;
import server.jdbc.JDBCException;
import server.utils.MockUtil;

public class HttpServiceImpl implements IHttpService{

	@Override
	@Deprecated
	public void addBroadCast(String message) {
		// TODO Auto-generated method stub
		System.out.println("do something on server side "+message);
	}

	@Override
	public void addNotice(Notice notice) {
		InformationData data =new InformationData();
		data.setId(notice.getId());
		data.setAreaId(notice.getAreaId());
		data.setMessage(notice.getMessage());
		data.setStatus(notice.getStatus());
		data.setInterval(notice.getInterval());
		data.setCreatedDate(notice.getCreatedDate());
		data.setCreaterId(notice.getCreaterId());
		data.setCloseDate(notice.getCloseDate());
		data.send();
		ServerEngin.getInstance().getCache().getInformationManager().getNoticeMap().put(data.getId(), data);
	}

	

	@Override
	public void updateNotice(Notice notice) {
		InformationData data =new InformationData();
		data.setId(notice.getId());
		data.setAreaId(notice.getAreaId());
		data.setMessage(notice.getMessage());
		data.setStatus(notice.getStatus());
		data.setInterval(notice.getInterval());
		data.setCreatedDate(notice.getCreatedDate());
		data.setCreaterId(notice.getCreaterId());
		data.setCloseDate(notice.getCloseDate());
		data.send();
		ServerEngin.getInstance().getCache().getInformationManager().getNoticeMap().put(data.getId(), data);
	}

	@Override
	public List<Notice> showCurrentList() {
		List<Notice> list = new ArrayList<Notice>();
		Collection<InformationData> colltions = ServerEngin.getInstance().getCache().getInformationManager().getNoticeMap().values();
		for(InformationData data:colltions)
		{
			Notice notice = new Notice();
			notice.setId(data.getId());
			notice.setAreaId(data.getAreaId());
			notice.setMessage(data.getMessage());
			notice.setStatus(data.getStatus());
			notice.setInterval(data.getInterval());
			notice.setCreatedDate(data.getCreatedDate());
			notice.setCreaterId(data.getCreaterId());
			notice.setCloseDate(data.getCloseDate());
			list.add(notice);
		}
		return list;
	}

	@Override
	@Deprecated
	public List<Notice> showAllNotices() {
		List<Notice> list = new ArrayList<Notice>();
		Collection<InformationData> colltions = ServerEngin.getInstance().getCache().getInformationManager().getNoticeMap().values();
		for(InformationData data:colltions)
		{
			Notice notice = new Notice();
			notice.setId(data.getId());
			notice.setAreaId(data.getAreaId());
			notice.setMessage(data.getMessage());
			notice.setStatus(data.getStatus());
			notice.setInterval(data.getInterval());
			notice.setCreatedDate(data.getCreatedDate());
			notice.setCreaterId(data.getCreaterId());
			notice.setCloseDate(data.getCloseDate());
			list.add(notice);
		}
		return list;
	}

	@Override
	public List<User> showAllUsers() {
	    List <User> list = new ArrayList<User>();
	    
	    
	    
	    
//	    private int id;
//		private String name;
//		private String password;
//		private String email;
//		private String nickName; // 昵称
//		private String signature; // 个性签名
//		private boolean robot; // 是否机器人
//		private int diffLevel; // 机器人难度等级
//		private boolean freeze; // 是否禁止登录
//		private boolean shutup; // 是否禁言
//		private boolean online; // 是否在线
//		private String title; // 头衔
//		private int score; // 积分
//		private int level; // 等级
//		private int vipLevel; // vip等级
//		private Date vipDate; // vip期限
//		private int winCount; // 胜利次数
//		private int loseCount; // 失败次数
//		private int achieveValue; // 成就值
//		private Map<String, Long> wallet; // 钱包
	    
	    
        for(int i=0;i<15;i++){
            User u = new User();
            u.setId(i);
            u.setName("nimei==+"+i);
            u.setEmail("nimei@"+i+".com");
            u.setNickName("nickName"+i);
            u.setPassword("password"+i);
            u.setRobot(false);
            u.setTitle("title");
            list.add(u);
        }
        return list;
	}

	@Override
	public List<User> showOnlineUsers() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User getUserById(String userId) {
	    User user = MockUtil.randomUserList(1).get(0);
	    
	    return user;
	}

	@Override
	public List<Achievement> getAchievementById(String userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Item> getItemById(String userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void kickOutById(String userId) 
	{
		server.commonData.User user = ServerEngin.getInstance().getCache().getUser(userId);
		if(user==null)
			return;
		ILoginScript scripte = ServerEngin.getInstance().getScriptM().getScript(ScriptEnum.LOGIN_SCRIPT);
		scripte.quiteGame(user,true);
	}

	@Override
	public void shutup(String userId) {
		server.commonData.User user = ServerEngin.getInstance().getCache().getUser(userId);
		if(user==null)
			return;
		user.setShutup(true);
	}

	@Override
	public void freezeById(String userId) {
		server.commonData.User user = ServerEngin.getInstance().getCache().getUser(userId);
		if(user==null)
			return;
		user.setFreeze(true);
		ILoginScript scripte = ServerEngin.getInstance().getScriptM().getScript(ScriptEnum.LOGIN_SCRIPT);
		scripte.quiteGame(user,true);
	}

	@Override
	@Deprecated
	public boolean resetPassword(String userId, String password) {
		// TODO Auto-generated method stub
	    
	    return true;
		
	}

	@Override
	public boolean increaseCoins(String userId,String currency, long gold) {
		server.commonData.User user = ServerEngin.getInstance().getCache().getUser(userId);
		if(user==null)
			return false;
		user.addMoney((int)gold);
		//刷新信息给客户端
		ILoginScript scripte = ServerEngin.getInstance().getScriptM().getScript(ScriptEnum.LOGIN_SCRIPT);
		scripte.refrechVipMoneyGoldInfo(user);
		
		return true;
	}

	@Override
	public boolean decreaseCoins(String userId,String currency, long gold) {
		server.commonData.User user = ServerEngin.getInstance().getCache().getUser(userId);
		if(user==null)
			return false;
		user.delMoney(-(int)gold);
		//刷新信息给客户端
		ILoginScript scripte = ServerEngin.getInstance().getScriptM().getScript(ScriptEnum.LOGIN_SCRIPT);
		scripte.refrechVipMoneyGoldInfo(user);
		return true;
	}

	@Override
	public void upgradeVIP(String userId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void degradeVIP(String userId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void upgrade(String userId, String level) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void degrade(String userId, String level) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<User> richList(String currency) {
		// TODO Auto-generated method stub
	    List<User> list = MockUtil.randomUserList(10);
		return list;
	}

	@Override
	public List<User> scoreList() {
	    List<User> list = MockUtil.randomUserList(10);
        return list;
	}

	@Override
	public List<User> achieveList() {
	    List<User> list = MockUtil.randomUserList(10);
        return list;
	}

	@Override
	public List<User> winRatioList() {
	    List<User> list = MockUtil.randomUserList(10);
        return list;
	}

	@Override
	public List<Activity> showActivityList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Lottery> showLotteryList(String activityId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void releaseTalk(String userId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void releaseLogin(String userId) {
		// TODO Auto-generated method stub
		
	}

    @Override
    public boolean deleteNoticeById(String noticeId) {
        
        
        if (noticeId.equals("19")){
            
            return true;
        }
        
        return false;
    }

    @Override
    public User getUserByName(String userName) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void increaseScore(String userId, long score) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void decreaseScore(String userId, long score) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void updateVIPDate(String userId, Date vipDate) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public long recharge(String userId, String currency, long coins) {
    	server.commonData.User user = ServerEngin.getInstance().getCache().getUser(userId);
		if(user==null)
		{
			int nowYuanbao =-1;
			try
			{
				//玩家不在线 修改数据库
				DAOUtil dao = DAOUtil.getInstance();
				List<String> params = new ArrayList<String>();
				params.add(userId);
				@SuppressWarnings("unchecked")
				List<Map<String, Object>> result = dao.query(SqlStatementEnum.QUERY_USERBYACCOUNT, params);
				if(result.isEmpty())
					return -1;
				Map<String, Object> map = result.get(0);
				nowYuanbao = Utils.readInt(map, "yuanbao");
				nowYuanbao+=coins;
				int rechargeGold = Utils.readInt(map, "rechargedGold");
				rechargeGold+=coins;
				params.clear();
				params.add(String.valueOf(nowYuanbao));
				params.add(String.valueOf(rechargeGold));
				params.add(String.valueOf(userId));
				dao.execute(SqlStatementEnum.UPDATE_GOLD, params);
			} catch (JDBCException e) {
				e.printStackTrace();
			}
			return nowYuanbao;
		}
		user.addDelYuanbao((int)coins);
		user.setRechargedGold(user.getRechargedGold()+(int)coins);
		//刷新信息给客户端
		ILoginScript scripte = ServerEngin.getInstance().getScriptM().getScript(ScriptEnum.LOGIN_SCRIPT);
		scripte.refrechVipMoneyGoldInfo(user);
        return user.getYuanbao();
    }
}
